#include "job.h"
#include "translationrules.h"

Job::Job(QObject *parent) :
    QObject(parent){
    m_rules = 0;
}

void Job::setOutput(const QString& output, bool singleOutput){
    m_output = output;
    m_singleOutput = singleOutput;
}

void Job::addFile(const QString& filePath){
    if(!m_filePaths.contains(filePath))
        m_filePaths.append(filePath);
}

void Job::addFiles(const QStringList& filePaths){
    foreach(QString filePath, filePaths){
        addFile(filePath);
    }
}


void Job::removeFile(const QString& filePath){
    if(m_filePaths.contains(filePath))
        m_filePaths.removeOne(filePath);
}

void Job::removeFiles(const QStringList& filePaths){
    foreach(QString filePath, filePaths){
        removeFile(filePath);
    }

}
void Job::setRules(TranslationRules *rules){
    m_rules = rules;
}

bool Job::run(){
    if(m_rules){
        if(m_singleOutput){
            QFile output(m_output);
            if(!output.open(QFile::WriteOnly|QFile::Truncate))
                return false;
            output.close();
            foreach(QString filePath, m_filePaths){
                m_rules->process(filePath,m_output);
            }
        }
        else{
            foreach(QString filePath, m_filePaths){
                QFileInfo info(filePath);
                QString outputFile = info.absolutePath()+ info.baseName()+"."+m_output;
                QFile output(outputFile);
                if(!output.open(QFile::WriteOnly|QFile::Truncate))
                    return false;
                output.close();
                m_rules->process(filePath,outputFile);
            }
        }
        return true;
    }
    return false;
}
